package com.zsh.web;

import com.google.gson.Gson;
import com.zsh.pojo.User;
import com.zsh.service.UserService;
import com.zsh.service.impl.UserServiceImpl;
import com.zsh.utils.WebUtils;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.http.HttpRequest;
import java.util.HashMap;
import java.util.Map;

import static com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY;

public class UserServlet extends BaseServlet {
    UserService userService = new UserServiceImpl();

        protected void ajaxExistsUsername(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //获取请求参数
            String username = request.getParameter("username");

            boolean existsUsername = userService.existsUsername(username);

            Map<String, Object> resultMap = new HashMap<>();

            resultMap.put("existsUsername",existsUsername);

            Gson gson = new Gson();
            String json = gson.toJson(resultMap);

            response.getWriter().write(json);


        }

        /**
         * 注销
         * @param request
         * @param response
         * @throws ServletException
         * @throws IOException
         */
        protected void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            System.out.println(1);
            request.getSession().invalidate();
            System.out.println(request.getContextPath());
            response.sendRedirect(request.getContextPath());


        }

        protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取请求参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //2.判断用户名密码是否正确

        User loginUser = WebUtils.copyParamToBean(request.getParameterMap(),new User());
        User user = userService.login(loginUser);


            if(user == null){
            System.out.println("登录失败");
            //把错误信息和回显信息发送给jsp
            request.setAttribute("msg","用户名或密码错误！");
            request.setAttribute("username",username);

            request.getRequestDispatcher("/pages/user/login.jsp").forward(request,response);
        }else{
            System.out.println("登录成功");
            request.getSession().setAttribute("user",user);
            request.getRequestDispatcher("/pages/user/login_success.jsp").forward(request,response);
        }
    }

    protected void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取请求参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String email = request.getParameter("email");
        String code = request.getParameter("code");


        User user = WebUtils.copyParamToBean(request.getParameterMap(),new User());

        String token = (String) request.getSession().getAttribute(KAPTCHA_SESSION_KEY);

        request.getSession().removeAttribute(KAPTCHA_SESSION_KEY);
        //2.验证码验证，先写死 abcde
        if(token!=null && token.equalsIgnoreCase(code)){
            //3.检测用户名是否可用
            if(userService.existsUsername(username)){
                System.out.println("用户名" + username + "已存在");
                request.setAttribute("msg","用户名已存在！");
                request.setAttribute("username",username);
                request.setAttribute("email",email);
                request.getRequestDispatcher("/pages/user/regist.jsp").forward(request,response);
            }else {
                userService.registerUser(user);
                request.getRequestDispatcher("/pages/user/regist_success.jsp").forward(request,response);
            }
        }else{
            //不正确
            //跳回注册页面
            System.out.println("验证码" + code + "错误！");
            request.setAttribute("msg","验证码错误！");
            request.setAttribute("username",username);
            request.setAttribute("email",email);
            request.getRequestDispatcher("/pages/user/regist.jsp").forward(request,response);
        }
    }



    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
